function [model, model_ret, model_perf] = benchmark_returns(startdate, enddate, data_path, yahoo_tickers)
% TS = benchmark_returns(data_path, yahoo_tickers, startdate, enddate) 
% Calculate benchmark returns for a date window
% If arguments are blank, use defaults below
%

if nargin < 4, yahoo_tickers = {'ACWI','AGG'}; end
if nargin < 3, data_path = [getenv('wcm_root') 'data/benchmarks/']; end
if nargin < 2, enddate = mdt2TSdt(today()); end
if nargin < 1, startdate = 19940808; end

idx = readTS(data_path, 'index_levels.csv');
idx_monthly = dlyTS2monthly(idx);
idx_ret = price2returnTS(idx);

if enddate > idx_ret.dates(end)
  yahoo_data_loader(yahoo_tickers, TSdatenum(idx_ret.dates(end)), TSdatenum(enddate), data_path);
  all_ret = readTS(data_path, 'all_ret.csv');
  all_price = return2priceTS(all_ret, idx.data(end,:));
  all_ret = joinTS(idx_ret, all_ret, 'row','union');
  all_price = joinTS(idx, all_price, 'row','union');
else
  all_ret = idx_ret;
  all_price = idx;
end
monthly_ret = dlyTS2monthly(all_ret, 'sum');
model_names = {'aggressive','mod-aggressive','moderate','moderate_50-50','conservative-mod','conservative','ultra-conservative'};
eq_wgt = [1 0.8 0.6 0.5 0.4 0.2 0];
fi_wgt = 1-eq_wgt;
model = buildTS([],model_names,all_ret.dates);
eq_share = model;
fi_share = model;
model_ret = model;

model.data(1,:) = 100;
eq_share.data(1,:) = 100*eq_wgt./all_price.data(1,1);
fi_share.data(1,:) = 100*fi_wgt./all_price.data(1,2);
for i = 2:length(all_price.dates)
  %rebalancing
%  if mod(all_price.dates(i),100)<mod(all_price.dates(i-1),100)  %new month
%  if mod(all_price.dates(i),10000)<mod(all_price.dates(i-1),10000)  %new year
  if mod(mod(all_price.dates(i),10000),300)<mod(mod(all_price.dates(i-1),10000),300)  %new quarter
    eq_share.data(i,:) = model.data(i-1,:).*eq_wgt./all_price.data(i-1,1);
    fi_share.data(i,:) = model.data(i-1,:).*fi_wgt./all_price.data(i-1,2);
  else
    eq_share.data(i,:) = eq_share.data(i-1,:);
    fi_share.data(i,:) = fi_share.data(i-1,:);
  end  
  model.data(i,:) = all_price.data(i,1)*eq_share.data(i,:) + all_price.data(i,2)*fi_share.data(i,:);
end
model_ret = price2returnTS(model);
model_monthly_ret = dlyTS2monthly(model_ret, 'sum');

m = floor(all_ret.dates/10000)*12+mod(floor(all_ret.dates/100),100);
q = floor((m-1)/3);
y = floor(all_ret.dates/10000);

curr_month = m(end);
curr_qtr  = q(end);
curr_year = y(end);

model_perf = buildTS([],model_names,{'YTD','QTD','MTD','priorQtr','priorMonth'},'rownames');
model_perf.data(1,:) = exp(sum(model_ret.data(y==curr_year,:)))-1;
model_perf.data(2,:) = exp(sum(model_ret.data(q==curr_qtr,:)))-1;
model_perf.data(3,:) = exp(sum(model_ret.data(m==curr_month,:)))-1;
model_perf.data(4,:) = exp(sum(model_ret.data(q==curr_qtr-1,:)))-1;
model_perf.data(5,:) = exp(sum(model_ret.data(m==curr_month-1,:)))-1;

writeTS(model_perf,data_path,['Performance/model_perf_' num2str(all_ret.dates(end)) '.csv']);
writeTS(model_ret,data_path,'model_ret.csv');
writeTS(model,data_path,'model.csv');
writeIASprices(model, data_path, startdate, enddate);
return